package com.zeus.base.smscenter.controller;

import com.zeus.base.privilege.utils.StringUtil;
import com.zeus.base.smscenter.common.BaseController;
import com.zeus.base.smscenter.utils.LogUtils;
import com.zeus.base.privilege.model.TbUser;
import com.zeus.base.privilege.service.IUserService;
import com.zeus.base.privilege.utils.UserAgentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

/**
 * Created by ShanFei on 2017-04-01.
 */
@RestController
@RequestMapping("/")
public class LoginController extends BaseController {
    @Autowired
    private IUserService iUserService;
    @RequestMapping(value = "userLogin", method = RequestMethod.POST)
    public ModelAndView userLogin(HttpServletRequest request){
        TbUser user = (TbUser) request.getSession().getAttribute("sessionUser");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:main.html");
        if (user!=null) {
            user = iUserService.selectByPrimaryKey(user.getUserId());
        }
        user.setOldLoginIp(user.getLoginIp()==null ? "":user.getLoginIp());
        user.setOldLoginTime(user.getOldLoginTime()==null ? null:user.getOldLoginTime());
        user.setLoginIp(UserAgentUtils.getRemoteAddr(request));
        user.setLoginTime(new Date());
        iUserService.update(user);
        LogUtils.saveLog(request,"",user.getUserId().toString(),user.getUserCode(),"UserManager","userLogin","用户登陆","");
        return modelAndView;
    }
}
